package com.stmt.analytic.graph.algorithm;

import java.util.Objects;

public class Pair<A, B> {

    public final A fst;
    public final B snd;

    public final double weight;

    public Pair(A fst, B snd, double weight) {
        this.fst = fst;
        this.snd = snd;
        this.weight = weight;
    }

    public Pair(A fst, B snd) {
        this.fst = fst;
        this.snd = snd;
        this.weight = 1.0d;
    }

    public String toString() {
        return "Pair[" + fst + "," + snd + "," + weight + "]";
    }

    public boolean equals(Object other) {
        return
                other instanceof Pair &&
                        Objects.equals(fst, ((Pair) other).fst) &&
                        Objects.equals(snd, ((Pair) other).snd) &&
                        (weight == ((Pair) other).weight);
    }

    public int hashCode() {
        if (fst == null) return (snd == null) ? 0 : snd.hashCode() + 1;
        else if (snd == null) return fst.hashCode() + 2;
        else return fst.hashCode() * 17 + snd.hashCode();
    }

    public static <A, B> Pair<A, B> of(A a, B b, double weight) {
        return new Pair<>(a, b, weight);
    }

    public static <A, B> Pair<A, B> of(A a, B b) {
        //default weight value is 1.0
        return new Pair<>(a, b, 1.0d);
    }
}